<template>
	<view class="userphone" v-if="toggle">
		<view class="content">
			<view class="logo box box-pack-center box-align-center">
				<image src="/static/images/logo.jpg" mode="aspectFit" class="img_nav"></image>
				<view class="txt">手机号登录/注册</view>
			</view>
			<view class="close box box-align-center box-pack-center" @tap="closePhone">
				<image src="/static/images/icon_close2.png"></image>
			</view>
			<view class="con box box-pack-center box-align-center">
				<!-- #ifdef MP-WEIXIN -->
				<button class="mini-btn auth-btn box box-pack-center box-align-center" open-type='getPhoneNumber'
				 @getphonenumber="getPhoneNumber" v-if="!phoneloading">
					<image src="/static/images/icon_phone.png"></image>
					<view class="txt">授权</view>
				</button>
				<button class="kfbt mini-btn auth-btn box box-pack-center box-align-center" v-else>
					<image src="/images/icon_phone.png"></image>
					<view class="txt">正在登录...</view>
				</button>
				<!-- #endif -->  
			</view>
		</view>
		<view class="make"></view>
	</view>
</template>

<script>
	// import bus from '../../common/bus'
	export default {
		data() {
			return {
				toggle: this.phoneToggle
			}
		},
		props: {
			phoneToggle: {
				type: Boolean,
				default: false
			},
		},
		watch:{
			phoneToggle(){
				this.toggle = this.phoneToggle
			}
		},
		methods: {
			getPhoneNumber(e) {
				let that = this;
				console.log(123456789)
				uni.login({
					success: res => {
						var zdy = {
							'encryptedData': e.detail.encryptedData,
							'iv': encodeURIComponent(e.detail.iv),
							'code': res.code,
							'openid': uni.getStorageSync('openid')
						}
						that.$http.post("Auth/decryptWxPhoneNew", zdy).then(json=>{
							console.log(json);
							let data = json.data
							if (json.success == 200) { //我后台设置的返回值为200是正确
								//存入缓存即可
								if (data) {
									uni.setStorageSync('user_phone', data.phoneNumber)
								}
								let info = {
									'openid': uni.getStorageSync('openid'),
									'phone': data.phoneNumber
								}
								that.appRegister(info, () => {
									typeof callback == 'function' && callback();
								});
							} else {
								typeof callback == 'function' && callback();
								// that.alertMsg(json.msg)
							}
						})
					}
				})
			},
			// 注册生成会员
			appRegister(info, callback) {
				let that = this;
				let share_openid = wx.getStorageSync('share_openid')
				if (share_openid) {
					info.share_openid = share_openid
				}
				that.$http.post('Auth/register', info, function(json) {
					console.log('注册会员是否成功', json)
					if (json.code === 200) {
						uni.setStorageSync('is_vip', true)
						this.closePhone()
						typeof callback === 'function' && callback();
					} else {
						uni.showToast({
							title: json.msg
						});
					}
				})
			},
			// 关闭弹窗
			closePhone(){
				this.toggle = false
				this.$emit('setToggle', this.toggle)
			}
		}
	}
</script>

<style lang="scss">
	/* 手机号码授权 */

	.userphone {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		z-index: 88888;
	}

	.userphone .make {
		position: absolute;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, 0.5);
	}

	.userphone .content {
		position: absolute;
		top: 50%;
		left: 7%;
		width: 80%;
		height: 340rpx;
		margin-top: -200rpx;
		z-index: 55;
		background: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		padding: 20rpx 3%;
	}

	.userphone .content .logo {
		flex-flow: column nowrap;
	}

	.userphone .content .logo .img_nav {
		width: 120rpx;
		height: 120rpx;
	}

	.userphone .content .logo .img_nav image {
		width: 120rpx;
		height: 120rpx;
	}

	.userphone .content .logo .txt {
		font-size: 36rpx;
		line-height: 56rpx;
		color: #080808;
	}

	.userphone .content .close {
		position: absolute;
		top: 0rpx;
		right: 0rpx;
		width: 100rpx;
		height: 100rpx;
	}

	.userphone .content .close image {
		width: 40rpx;
		height: 40rpx;
	}



	.userphone .auth-btn {
		width: 670rpx;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #0bb908;
		margin-top: 50rpx;
		border-radius: 5px;
	}



	.userphone .auth-btn image {
		width: 30rpx;
		height: 48rpx;
	}

	.userphone .auth-btn .txt {
		font-size: 32rpx;
		color: #fff;
		margin-left: 24rpx;
	}
</style>
